﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Twi.COMMON.Core
{
    public class TwiEnumHelper
    {
        /// <summary>
        /// 获取枚举的名称
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetName(Enum t)
        {
            return t.ToString();            
        }

        /// <summary>
        /// 获取枚举的值
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int GetValue(Enum t)
        {
            return Convert.ToInt32(t);
        }

        /// <summary>
        /// 根据枚举常数的名称或数字值的字符串，获取枚举对象
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="nameOrValue">枚举常数的名称或数字值的字符串</param>
        /// <returns></returns>
        public static T GetEnumByNameOrValue<T>(string nameOrValue) //where T : Enum
        {
            return (T)Enum.Parse(typeof(T), nameOrValue, false);
        }
    }
}
